function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function (milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function (dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function (number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}
/*
 * 获取浏览器版本信息
 */
function getBrowerInfo () {
  const userAgent = navigator.userAgent.toLowerCase()
  let browserType = ''
  let browserVersion = ''
  // 浏览器类型-IE
  if (userAgent.match(/msie/) != null || userAgent.match(/trident/) != null) {
    browserType = 'ie'
    browserVersion = getIeVersion()
  } else if (window.opera || (userAgent.indexOf('opr') > 0)) {
    // 欧朋
    browserType = 'opr'
    browserVersion = getOperaVersion(userAgent)
  } else if (userAgent.indexOf('ubrowser') > 0) {
    browserType = 'uc'
    browserVersion = userAgent.match(/ubrowser\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('bidubrowser') > 0) {
    // 百度
    browserType = 'baidu'
    browserVersion = userAgent.match(/bidubrowser\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('metasr') > 0 || userAgent.indexOf('se 2.x') > 0) {
    // 搜狗
    browserType = 'metasr'
    // browserVersion =  userAgent.match(/metasr\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('tencenttraveler') > 0) {
    browserType = 'qq'
    browserVersion = userAgent.match(/tencenttraveler\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('qqbrowser') > 0) {
    browserType = 'qq'
    browserVersion = userAgent.match(/qqbrowser\/([\d.]+)/)[1]
  } else if (userAgent.match(/MicroMessenger/i) == "micromessenger" || userAgent.match(/WeiBo/i) == "weibo") {
	browserType = 'weixin'
  } else if(userAgent.match(/AlipayClient/i) == "alipayclient"){
	browserType = 'alipay'//支付宝
  } else if(userAgent.indexOf('dtdreamweb') > -1){
	browserType = 'zlb'//浙里办app
  } else if(userAgent.indexOf('miniprogram') > -1 && userAgent.indexOf('alipay') > -1){
	browserType = 'zlb-alipay'//浙里办小程序
  }else if (userAgent.indexOf('maxthon') > 0) {
    // 遨游
    browserType = 'maxthon'
    browserVersion = userAgent.match(/maxthon\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('firefox') > 0) {
    // 火狐
    browserType = 'firefox'
    browserVersion = userAgent.match(/firefox\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('edge') > 0) {
    // edge
    browserType = 'edge'
    browserVersion = userAgent.match(/edge\/([\d.]+)/)[1]
  } else if (userAgent.indexOf('chrome') > 0) {
    // 谷歌/360
    if (validate360('type', 'application/x360mmplugin')) {
      browserType = '360'
      // browserVersion =  userAgent.match(/chrome\/([\d.]+)/)[1]
    } else {
      browserType = 'chrome'
      browserVersion = userAgent.match(/chrome\/([\d.]+)/)[1]
    }
  } else if (userAgent.indexOf('Safari') > -1) {
    // 苹果
    browserType = 'safari'
    browserVersion = userAgent.match(/version\/([\d.]+)/)[1]
  } else {
    browserType = 'other'
  }
  const browser = {}
  browser.browserType = browserType
  browser.browserVersion = browserVersion
  return browser
}
/*
 * 判断是否为360浏览器
 */
function validate360 (option, value) {
  const mimeTypes = window.navigator.mimeTypes
  for (const mt in mimeTypes) {
    console.log(mimeTypes[mt][option])
    if (mimeTypes[mt][option] === value) {
      return true
    }
  }
  return false
}
/*
 * 获取IE浏览器版本
 */
function getIeVersion () {
  const IEMode = document.documentMode
  const rMsie = /(msie\s|trident.*rv:)([\w.]+)/
  const ma = window.navigator.userAgent.toLowerCase()
  const match = rMsie.exec(ma)
  try {
    return match[2]
  } catch (e) {
    return IEMode
  }
}
/*
 * 获取oper浏览器版本
 */
function getOperaVersion (userAgent) {
  try {
    if (window.opera) {
      return userAgent.match(/opera.([\d.]+)/)[1]
    } else if (userAgent.indexOf('opr') > 0) {
      return userAgent.match(/opr\/([\d.]+)/)[1]
    }
  } catch (e) {
    return 0
  }
}
function isWechatOrQQ() {
	var ua = navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger" || ua.match(/WeiBo/i) == "weibo") {
		return true;
	} else if (ua.indexOf('mobile mqqbrowser') > -1) {
		return true;
	} else if (ua.indexOf('iphone') > -1 || ua.indexOf('mac') > -1) {
		if (ua.indexOf('qq') > -1) {
			return true;
		}

	}
	return false;
}
function distance(la1, lo1, la2, lo2) {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
s = s * 6378137;//地球半径 米
s = Math.round(s * 10000) / 10000;
console.log("计算结果",s);
return s;
}
function getQueryString(name){
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    const search = window.location.search.split('?')[1] || '';
	if(search){
		const r = search.match(reg) || [];
		return r[2]
	}else{
		return null
	}
    
}
function decodePath(path){
	path=path.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%26/g,"&").replace(/%3D/g,"=").replace(/%23/g,"#").replace(/%25/g,"%")
	return path
}
function judge() {
    const os = function () {
        var ua = navigator.userAgent,
            isWindowsPhone = /(?:Windows Phone)/.test(ua),
            isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
            isAndroid = /(?:Android)/.test(ua),
            isFireFox = /(?:Firefox)/.test(ua),
            isChrome = /(?:Chrome|CriOS)/.test(ua),
            isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
            isPhone = /(?:iPhone)/.test(ua) && !isTablet,
            isPc = !isPhone && !isAndroid && !isSymbian;
        return {
            isTablet: isTablet,
            isPhone: isPhone,
            isAndroid: isAndroid,
            isPc: isPc
        };
    }();
    if (os.isAndroid || os.isPhone) {
        // alert("手机")
        return 'mobile'
    } else if (os.isTablet) {
        return 'pad'
    } else if (os.isPc) {
        return 'pc'
    }
}
module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	uuid:uuid,
	isWechatOrQQ:isWechatOrQQ,
	distance:distance,//根据经纬度计算距离
	getQueryString:getQueryString,//获取url参数
	getBrowerInfo:getBrowerInfo,//获取浏览器类型
	decodePath:decodePath,//转义url
	judge:judge//获取设备类型
}
